package com.curvebreakers.wheresmystuff.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.telephony.PhoneNumberUtils;

/**
 * Represents a user of the application.
 * @author evan.cahill
 */
public class User {
	private int id;
	private String email, password, phoneNum;
	private boolean admin, locked;
	
	private User(int id, String email, String password, String phoneNum, boolean admin, boolean locked) {
		this.id = id;
		this.email = email;
		this.password = password;
		this.phoneNum = PhoneNumberUtils.formatNumber(phoneNum);
		this.admin = admin;
		this.locked = locked;
	}
	
	public User(String email, String password, String phoneNum) {
		this(-1, email, password, phoneNum, false, false);
	}
		
	public User(int idNum, String email, String phoneNum, boolean admin, boolean locked) {
		this(idNum, email, null, phoneNum, admin, locked);
	}
	
	/**
	 * Creates a User object from a JSONObject retrieved from the database
	 * @param obj a JSONObject
	 * @return the corresponding user
	 */
	public static User createFromJSON(JSONObject obj){
		try {
			return new User(obj.getInt("id"), obj.getString("email"), obj.getString("phone_number"), obj.getString("admin").equals("t") ? true : false, obj.getString("locked").equals("t") ? true : false);
		} catch (JSONException e) {
			System.out.println("User create from JSON error: "+e);
			return null;
		}
	}

	/**
	 * Iterates through a JSONArray retrieved from the database and creates 
	 * a User object from each element
	 * @param array the JSONArray
	 * @return a List of Users
	 */
	public static List<User> createListFromJSON(JSONArray array) {
		List<User> list = new ArrayList<User>(array.length());
		for (int i=0; i<array.length(); i++) {
			User user;
			try {
				user = User.createFromJSON(array.getJSONObject(i));
			} catch (JSONException e) {
				user = null;
				e.printStackTrace();
			}
			if (user!=null) list.add(user);
		}
		return list;
	}
	
	/**
	 * @return the User's email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @return the User's password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @return true if User is an admin, false otherwise
	 */
	public boolean isAdmin() {
		return admin;
	}

	/**
	 * @return true if User's account is locked, false otherwise
	 */
	public boolean isLocked() {
		return locked;
	}

	/**
	 * @param email a string to set the User's email
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @param password a string to set the User's password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @param admin a boolean to add or remove admin status from a user
	 */
	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	/**
	 * @param locked a boolean to lock or unlock the User's account
	 */
	public void setLocked(boolean locked) {
		this.locked = locked;
	}
	
	/**
	 * @return the User's id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @return the User's phone number
	 */
	public String getPhoneNum() {
		return phoneNum;
	}

	/**
	 * @param phoneNum a string to set the User's phone number
	 */
	public void setPhoneNum(String phoneNum) {
		this.phoneNum = PhoneNumberUtils.formatNumber(phoneNum);
	}
	
}
